<?php
use kartik\grid\GridView;
use yii\helpers\Html;
use common\models\Articlecat;
use common\models\Admin;
$this->registerCss(".kv-merged-header{border-bottom:1px solid #eee !important}");
$this->title = '文章列表';
$this->params['breadcrumbs'][] = ['label' => '文章管理', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$gridColumns = [
    ['class' => 'kartik\grid\SerialColumn'],//索引列
    [
        'class' => 'kartik\grid\ExpandRowColumn',//可以查看详情列
        'value'=>function ($model, $key, $index, $column) {
            return GridView::ROW_COLLAPSED;
        },
//        'detail'=> function($model,$key,$index,$column) {
//            $searchModel = new \backend\models\CnContactSearch();
//            $searchModel->cn_company_id = $model->id;
//            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
//            return Yii::$app->controller->renderPartial('/cn-contact/index',[
//                'searchModel'=> $searchModel,
//                'dataProvider'=>$dataProvider,
//            ]);
//        },
        'detail'=>function ($model, $key, $index, $column) {
            return Yii::$app->controller->renderPartial('detail', ['model'=>$model]);
        },
        'detailAnimationDuration'=>100,
        'headerOptions'=>['class'=>'kartik-sheet-style'],
        'expandOneOnly'=>true

    ],
    ['class' => 'kartik\grid\CheckboxColumn',//多选列
        'rowSelectedClass' => GridView::TYPE_INFO,//通用设置
        'visible'=>true,//不显示，代码也没有
        //'hidden'=>true,//隐藏，代码还有，导出csv等时还存在
        'hiddenFromExport'=>true,//虽然显示，但导出csv时忽略掉
        //'pageSummary'=>'总计',//可以是字符串，当为true时，自动合计
        'mergeHeader'=>true,//合并标题和检索栏
    ],
    ['attribute' => 'title'],
//    [
//        'class' => 'kartik\grid\EditableColumn',
//        'attribute' => 'title',
//        'pageSummary' => '合计',
//        'vAlign'=>'middle',
//        'headerOptions'=>['class'=>'kv-sticky-column'],
//        'contentOptions'=>['class'=>'kv-sticky-column'],
//        'editableOptions'=> function ($model, $key, $index) {
//            return [
//                'size'=>'md',
//                'formOptions' => [
//                    'method'=>'post',
//                    'action' =>  Yii::$app->urlManager->createAbsoluteUrl(['article/update2', 'id' => $model->id])
//                ]
//            ];
//        }
//    ],
    ['attribute'=>'cat_id','value'=>'catname.cat_name','filter'=>Articlecat::find()
        ->select(['cat_name','id'])
        ->indexBy('id')
        ->column(),
    ],
//    [
//        'class'=>'kartik\grid\BooleanColumn',
//        'attribute'=>'status.status',
//        'vAlign'=>'middle',
//    ],
    ['attribute'=>'updated_at','label'=>'最后修改时间','format'=>['date','php:Y-m-d H:i:s'],'pageSummary' => true,],
    ['attribute'=>'admin_id', 'label'=>'作者','value'=>'admin.name','filter'=>Admin::find()
        ->select(['name','id'])
        ->indexBy('id')
        ->column(),],
    [
        'class' => 'kartik\grid\ActionColumn',//动作列
        'dropdown' => false,
        'vAlign'=>'middle',
        'urlCreator' => function($action, $model, $key, $index) { return 'index.php?r=article/'.$action.'&id='.$key; },
        'viewOptions'=>['title'=>'显示', 'data-toggle'=>'tooltip'],
        'updateOptions'=>['title'=>'更新', 'data-toggle'=>'tooltip'],
        'deleteOptions'=>['title'=>'删除', 'data-toggle'=>'tooltip'],
    ],
];
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $gridColumns,
    'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
    'toolbar' =>  [
        ['content' =>
            Html::a('新增', ['article/create'], ['class' => 'btn btn-success'])
        ],
        '{export}',
        '{toggleData}'
    ],
    'pjax' => true,
    'bordered' => true,
    'striped' => false,
    'condensed' => false,
    'responsive' => true,
    'hover' => true,
    'floatHeader' => true,
    'floatHeaderOptions' => ['scrollingTop' => '23'],
    'showPageSummary' => true,
    'panel' => [
        'heading'=>'<i class="glyphicon glyphicon-book"></i> 列表数据',//不要了
        'before'=>'<div style="margin-top:8px">{summary}</div>',//放在before中，前面的div主要是想让它好看
        'type' => GridView::TYPE_PRIMARY //设置样式
    ],
    'toggleDataOptions'=>[
        'maxCount' => 200,//当超过200条时，此按钮隐藏，以免数据太多造成加载问题
        'minCount' => 5,//当现有总条数大于此值时,点击不会出现下方提示
        'confirmMsg' => '总共'. number_format($dataProvider->getTotalCount()).'条数据，确定要显示全部？',//点击时的确认
    ],
    'export'=>[
        //'fontAwesome'=>'fa fa-share-square-o',//图标
        'target'=>'_blank',//在新标签打开
        'encoding'=>'utf-8',//编码
    ],
//    'exportConfig' => [
//        GridView::CSV => [
//            'label' => '导出CSV',
//            'iconOptions' => ['class' => 'text-primary'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => '用户表'.date("Y-m-d"),
//            'alertMsg' => '确定要导出CSV格式文件？',
//            'options' => [
//                'title'=>'',
//            ],
//            'mime' => 'application/csv',
//            'config' => [
//                'colDelimiter' => ",",
//                'rowDelimiter' => "\r\n",
//            ],
//        ],
//    ],

//    $defaultExportConfig = [
//        GridView::HTML => [
//            'label' => Yii::t('kvgrid', 'HTML'),
//            'icon' => $isFa ? 'file-text' : 'floppy-saved',
//            'iconOptions' => ['class' => 'text-info'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => Yii::t('kvgrid', 'grid-export'),
//            'alertMsg' => Yii::t('kvgrid', 'The HTML export file will be generated for download.'),
//            'options' => ['title' => Yii::t('kvgrid', 'Hyper Text Markup Language')],
//            'mime' => 'text/html',
//            'config' => [
//                'cssFile' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'
//            ]
//        ],
//        GridView::CSV => [
//            'label' => Yii::t('kvgrid', 'CSV'),
//            'icon' => $isFa ? 'file-code-o' : 'floppy-open',
//            'iconOptions' => ['class' => 'text-primary'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => Yii::t('kvgrid', 'grid-export'),
//            'alertMsg' => Yii::t('kvgrid', 'The CSV export file will be generated for download.'),
//            'options' => ['title' => Yii::t('kvgrid', 'Comma Separated Values')],
//            'mime' => 'application/csv',
//            'config' => [
//                'colDelimiter' => ",",
//                'rowDelimiter' => "\r\n",
//            ]
//        ],
//        GridView::TEXT => [
//            'label' => Yii::t('kvgrid', 'Text'),
//            'icon' => $isFa ? 'file-text-o' : 'floppy-save',
//            'iconOptions' => ['class' => 'text-muted'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => Yii::t('kvgrid', 'grid-export'),
//            'alertMsg' => Yii::t('kvgrid', 'The TEXT export file will be generated for download.'),
//            'options' => ['title' => Yii::t('kvgrid', 'Tab Delimited Text')],
//            'mime' => 'text/plain',
//            'config' => [
//                'colDelimiter' => "\t",
//                'rowDelimiter' => "\r\n",
//            ]
//        ],
//        GridView::EXCEL => [
//            'label' => Yii::t('kvgrid', 'Excel'),
//            'icon' => $isFa ? 'file-excel-o' : 'floppy-remove',
//            'iconOptions' => ['class' => 'text-success'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => Yii::t('kvgrid', 'grid-export'),
//            'alertMsg' => Yii::t('kvgrid', 'The EXCEL export file will be generated for download.'),
//            'options' => ['title' => Yii::t('kvgrid', 'Microsoft Excel 95+')],
//            'mime' => 'application/vnd.ms-excel',
//            'config' => [
//                'worksheet' => Yii::t('kvgrid', 'ExportWorksheet'),
//                'cssFile' => ''
//            ]
//        ],
//        GridView::PDF => [
//            'label' => Yii::t('kvgrid', 'PDF'),
//            'icon' => $isFa ? 'file-pdf-o' : 'floppy-disk',
//            'iconOptions' => ['class' => 'text-danger'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => Yii::t('kvgrid', 'grid-export'),
//            'alertMsg' => Yii::t('kvgrid', 'The PDF export file will be generated for download.'),
//            'options' => ['title' => Yii::t('kvgrid', 'Portable Document Format')],
//            'mime' => 'application/pdf',
//            'config' => [
//                'mode' => 'c',
//                'format' => 'A4-L',
//                'destination' => 'D',
//                'marginTop' => 20,
//                'marginBottom' => 20,
//                'cssInline' => '.kv-wrap{padding:20px;}' .
//                    '.kv-align-center{text-align:center;}' .
//                    '.kv-align-left{text-align:left;}' .
//                    '.kv-align-right{text-align:right;}' .
//                    '.kv-align-top{vertical-align:top!important;}' .
//                    '.kv-align-bottom{vertical-align:bottom!important;}' .
//                    '.kv-align-middle{vertical-align:middle!important;}' .
//                    '.kv-page-summary{border-top:4px double #ddd;font-weight: bold;}' .
//                    '.kv-table-footer{border-top:4px double #ddd;font-weight: bold;}' .
//                    '.kv-table-caption{font-size:1.5em;padding:8px;border:1px solid #ddd;border-bottom:none;}',
//                'methods' => [
//                    'SetHeader' => [
//                        ['odd' => $pdfHeader, 'even' => $pdfHeader]
//                    ],
//                    'SetFooter' => [
//                        ['odd' => $pdfFooter, 'even' => $pdfFooter]
//                    ],
//                ],
//                'options' => [
//                    'title' => $title,
//                    'subject' => Yii::t('kvgrid', 'PDF export generated by kartik-v/yii2-grid extension'),
//                    'keywords' => Yii::t('kvgrid', 'krajee, grid, export, yii2-grid, pdf')
//                ],
//                'contentBefore'=>'',
//                'contentAfter'=>''
//            ]
//        ],
//        GridView::JSON => [
//            'label' => Yii::t('kvgrid', 'JSON'),
//            'icon' => $isFa ? 'file-code-o' : 'floppy-open',
//            'iconOptions' => ['class' => 'text-warning'],
//            'showHeader' => true,
//            'showPageSummary' => true,
//            'showFooter' => true,
//            'showCaption' => true,
//            'filename' => Yii::t('kvgrid', 'grid-export'),
//            'alertMsg' => Yii::t('kvgrid', 'The JSON export file will be generated for download.'),
//            'options' => ['title' => Yii::t('kvgrid', 'JavaScript Object Notation')],
//            'mime' => 'application/json',
//            'config' => [
//                'colHeads' => [],
//                'slugColHeads' => false,
//                'jsonReplacer' => null,
//                'indentSpace' => 4
//            ]
//        ],
//    ];
]);